// 统一响应格式工具

class ResponseHelper {
  // 成功响应
  static success(res, data = null, message = 'Success', statusCode = 200) {
    return res.status(statusCode).json({
      success: true,
      message,
      data,
      timestamp: new Date().toISOString()
    });
  }

  // 错误响应
  static error(res, message = 'Error', statusCode = 500, errors = null) {
    return res.status(statusCode).json({
      success: false,
      message,
      errors,
      timestamp: new Date().toISOString()
    });
  }

  // 分页响应
  static paginated(res, data, page, limit, total, message = 'Success') {
    const totalPages = Math.ceil(total / limit);
    const hasNextPage = page < totalPages;
    const hasPrevPage = page > 1;

    return res.status(200).json({
      success: true,
      message,
      data,
      pagination: {
        page,
        limit,
        total,
        totalPages,
        hasNextPage,
        hasPrevPage
      },
      timestamp: new Date().toISOString()
    });
  }

  // 创建成功响应
  static created(res, data = null, message = 'Created successfully') {
    return this.success(res, data, message, 201);
  }

  // 无内容响应
  static noContent(res, message = 'No content') {
    return res.status(204).json({
      success: true,
      message,
      timestamp: new Date().toISOString()
    });
  }

  // 验证错误响应
  static validationError(res, errors, message = 'Validation failed') {
    return this.error(res, message, 400, errors);
  }

  // 未授权响应
  static unauthorized(res, message = 'Unauthorized') {
    return this.error(res, message, 401);
  }

  // 禁止访问响应
  static forbidden(res, message = 'Forbidden') {
    return this.error(res, message, 403);
  }

  // 未找到响应
  static notFound(res, message = 'Resource not found') {
    return this.error(res, message, 404);
  }

  // 冲突响应
  static conflict(res, message = 'Conflict') {
    return this.error(res, message, 409);
  }

  // 服务器错误响应
  static serverError(res, message = 'Internal server error') {
    return this.error(res, message, 500);
  }
}

module.exports = ResponseHelper;
